package org.example.spring.controller.Admin;

import io.swagger.annotations.ApiOperation;
import org.example.spring.domain.pojo.Admin;
import org.example.spring.domain.pojo.Result;
import org.example.spring.service.AdminService;
import org.example.spring.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController("adminController")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    @PostMapping("/login")  //管理员登入
    @ApiOperation(value = "管理员登入")
    public Result login(@RequestBody Map<String,String> user){
        String username=user.get("username");
        String password=user.get("password");
        Admin admin= adminService.getAdmin(username,password);
        if(admin!=null){
            Map<String,Object>claims=new HashMap<>();
            claims.put("username",username);
            claims.put("password",password);
            String token= JwtUtil.genToken(claims);
            return Result.success(token);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

}
